package cn.sdormitory.controller;

import cn.sdormitory.common.api.CommonResult;
import cn.sdormitory.common.dto.ResultMsg;
import cn.sdormitory.service.AppWeChatService;
import com.alibaba.fastjson.JSONObject;
import io.jsonwebtoken.lang.Collections;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.io.IOException;
import java.util.Map;

/**
 * @author: lx
 * @date: 2023/11/15 11:29
 * @description:
 */
@RestController
@RequestMapping("/app/weChat")
public class AppWeChatController {

    @Autowired
    AppWeChatService appWeChatService;
    
    /**
     * 用户登记
     * @param paramMap
     * @return
     */
    @PostMapping("/getOpenID")
    public ResultMsg getOpenID(@RequestBody Map<String,String> paramMap) throws IOException {
        Map<String, String> wxOpenId = appWeChatService.getWxOpenId(paramMap);
        if(Collections.isEmpty(wxOpenId)){
            return ResultMsg.BY_FAIL("编码错误");
        }
        return ResultMsg.BY_SUCCESS("success", wxOpenId);
    }

    @PostMapping("/saveOpenId")
    public CommonResult saveOpenId(@RequestBody Map<String,String> map){
        String code = map.get("code");
        String role = map.get("role");
        int num = appWeChatService.saveOpenId(code,role);
        if(num > 0){
            return CommonResult.success();
        }else {
            return CommonResult.failed();
        }
    }
    
    /**
     * 获取手机号
     * @param paramMap
     * @return
     */
    @PostMapping("/getPhone")
    public ResultMsg getPhone(@RequestBody Map<String,String> paramMap)
    {
        JSONObject phoneInfo = appWeChatService.getPhone(paramMap);
        if(Collections.isEmpty(phoneInfo)){
            return ResultMsg.BY_FAIL("编码错误");
        }
        return ResultMsg.BY_SUCCESS("success", phoneInfo);
    }
}
